<?
$sub_menu = "400100";
include_once("./_common.php");
auth_check($auth[$sub_menu], "r");
$g4[title] = "상품관리";
include_once("./admin.head.php");

sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_qty` INT( 5 ) NOT NULL DEFAULT '1' AFTER `g_name`  ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_show` CHAR( 1 ) NOT NULL DEFAULT '1' AFTER `g_qty` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_order_flag` CHAR( 1 ) NOT NULL DEFAULT '1' AFTER `g_show` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_order` INT( 6 ) NOT NULL DEFAULT '0' AFTER `g_order_flag` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_icon` varchar( 50 ) NOT NULL AFTER `g_order` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_sell_price` int( 10 ) NOT NULL AFTER `g_info` ", false);

if ($stx)
{
    $sql_search = " and ( ";
    $sql_search .= " g_name like '%$stx%' ";
    $sql_search .= " )";
}

$cate1 = $cate1 ? $cate1 : "";

if($cate1 != "")
{
	if(!empty($cate_msg))
	{
		$sql_search .= " AND c_code='".$cate_msg."' ";
	}
	else
	{
		$sub_cate_sql = "	SELECT *
												FROM " . $g4['category_table'] . "
													WHERE c_pcode = '".$cate1."'";

		$sub_cate_rst = sql_query($sub_cate_sql);
		$tmp_cate = array();
		$_cate = "";

		while($sub_cate = mysql_fetch_array($sub_cate_rst))
		{
			$join_cate .= "'" . $sub_cate['c_code'] . "',";
			$tmp_cate[] = $sub_cate[c_code];
		}

		$join_cate = substr($join_cate, 0, strlen($join_cate)-2);

		if(count($tmp_cate))
		{
			$sql_search .= " AND c_code IN  (" . $join_cate . "') ";
		}
	}
}

$QUERY = "SELECT *
						FROM " . $g4['goods_table'] . "
							where 1 $sql_search";

if (!$sst) {
    $sst  = "idx";
    $sod = "desc";
}
$QUERY.= " order by $sst $sod ";

$total_count = mysql_num_rows(mysql_query($QUERY));

$rows = 30;
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$QUERY.=" limit $from_record, $rows";

$RESULT = mysql_query($QUERY);

$RowNum = $total_count - $from_record;

$row_count = mysql_num_rows(mysql_query($QUERY));
$pagelist = get_paging("10", $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&cate1=" . $cate1 . "&cate_msg=" . $cate_msg . "&page=");

$colspan = 11;
?>

<style type="text/css">
.red{color:#fe3600;}
.blue{color:#1734e8;}
</style>

<script type="text/javascript">
var list_update_php = "goods_list_update.php";
var list_delete_php = "goods_list_delete.php";
var list_move_php		= "goods_list_move.php";
</script>

<table cellpadding="0" cellspacing="1" border="0" width="100%">
	<colgroup>
		<col width="20">
		<col width="40">
		<col width="65">
		<col width="80">
		<col width="300">
		<col width="50">
		<col width="50">
		<col width="50">
		<col width="50">
		<col width="80">
		<col width="60">
	</colgroup>
	<form name=fsearch method=get>
	<tr>
		<td colspan="10">
			<span class="red">빨강색 : 주문 불가능</span> <span class="blue">파란색 : 진열하지않음</span>
		</td>
	</tr>
	<tr>
		<td colspan="4" align="left">
			카테고리 분류 :
			<!--대분류-->
			<select name="cate1" style="height:21px;font-family:Dotum;font-size:12px;color:#" onchange="get_category(this.value)">
				<option value="">전체</option>
				<?php
				$SQL = "SELECT *
									FROM " . $g4['category_table'] . "
										WHERE c_depth = '0'
											AND c_show='1' ";

				$result = sql_query($SQL);

				for ($i=0; $row = sql_fetch_array($result); $i++)
				{
				?>
				<option value="<?=$row['c_code']?>" <?=${'c_'.$row['c_code']}?> <?php if($cate1 == $row['c_code']) echo " selected='selected' "; ?>><?=$row['c_name']?></option>
				<?php
				}
				?>
			</select>
			<!--/대분류-->
		</td>
		<td colspan="2" align="left">
			<!--소분류-->
			<div id="cate_msg"></div>
			<!--소분류-->
		</td>
		<td colspan="5">
			검색 :
			<input type="text" name="stx" class="ed" style="width:120px" value="<?=$stx?>">
			<button class="btn1" style="cursor:pointer;height:20px;width:45px" onclick="submit();">검색</button>
		</td>
	</tr>
	</form>

	<form name="sAll" method="post" action="goods_update.php">
	<tr height="30" bgcolor="EFEFEF" align="center">
		<td><input type="checkbox" name="chkall" value="1" onclick="check_all(this.form)" /></td>
		<td>순번</td>
		<td>이미지</td>
		<td><?=subject_sort_link("g_cdoe")?>상품코드</a></td>
		<td><?=subject_sort_link("g_name")?>[카테고리] 상품명</a></td>
		<td><?=subject_sort_link("g_price")?>가격</a></td>
		<td><?=subject_sort_link("g_cnt")?>재고</a></td>
		<td><?=subject_sort_link("g_hit")?>조회수</a></td>
		<td><?=subject_sort_link("g_sale")?>판매수</a></td>
		<td><?=subject_sort_link("g_datetime")?>등록일</a></td>
		<td><img src='<?=$g4[admin_path]?>/img/icon_insert.gif' border=0 title='등록' style="cursor:pointer" onclick="location.href='./goods_form.php';"></td>
	</tr>
	<input type="hidden" name="w" value="SDEL">
	<?
	$row_no = 1;
	@mkdir($g4['path']."/data/goods/thum", 0707);
	@chmod($g4['path']."/data/goods/thum", 0707);

	while($row = mysql_fetch_array($RESULT))
	{
		if(!file_exists($g4['path']."/data/goods/thum/" . $row['idx']))
		{
			//img_resize( $g4['path']."/data/goods/" . $row['g_code'] . "_1.jpg" , 60 , $g4['path']."/data/goods/thum/" , $row['idx']);
		}

		$style = "";

		if($row['g_show'] == 2)
		{
			$style = "blue";
		}

		if($row['g_order_flag'] == 2)
		{
			$style = "red";
		}
	?>
	<tr height="25" align="center" onmouseover="this.style.background='#CCFFFF'" onmouseout="this.style.background='#FFFFFF'">
		<td><input type="checkbox" id="sidx_<?=$row_no?>" name="chk[]" value="<?=$row[idx]?>" /></td>
		<td><?=number_format($RowNum,0)?></td>
		<!-- <td><img src="<?=$g4['path']?>/data/goods/thum/<?=$row['idx']?>" /></td> -->
		<td><img src="<?=$g4['path']?>/data/goods/<?=$row['g_code']."_1.jpg"?>" /></td> 

		<td><?=$row[g_code]?></td>
		<td align="left"><span class="<?=$style?>">[<?=$row[c_code]?>] <?=$row[g_name]?></span></td>
		<td><?=number_format($row[g_price])?></td>
		<td><?=number_format($row[g_cnt])?></td>
		<td><?=number_format($row[g_hit])?></td>
		<td><?=number_format($row[g_sale])?></td>
		<td><?=date("Y-m-d",strtotime($row[g_datetime]))?></td>
		<td><img src='<?=$g4[admin_path]?>/img/icon_modify.gif' border=0 title='수정' style="cursor:pointer" onclick="location.href='./goods_form.php?w=UPDATE&IDX=<?=$row[idx]?>'">
		<img src='<?=$g4[admin_path]?>/img/icon_delete.gif' border=0 title='삭제' style="cursor:pointer" onclick="location.href='./goods_update.php?w=DELETE&IDX=<?=$row[idx]?>'"></td>
	</tr>
	<tr><td colspan="<?=$colspan?>" height="1" bgcolor="#d7d7d7"></td></tr>
	<?
		$RowNum--;
		$row_no++;
	}
	?>
	<tr><td colspan="<?=$colspan?>" height="40" align="right"><?=$pagelist?></td></tr>
</table>
<div>
	<span style="float:left;width:280px;">
		선택한 상품을 :
		<select name="move_cate" style="height:21px;font-family:Dotum;font-size:12px;color:#" onchange="get_category(this.value,'','cate_msg2')">
			<?php
			$SQL = "SELECT *
								FROM " . $g4['category_table'] . "
									WHERE c_depth = '0'
										AND c_show='1' ";

			$result = sql_query($SQL);

			for ($i=0; $row = sql_fetch_array($result); $i++)
			{
			?>
			<option value="<?=$row['c_code']?>" <?=${'c_'.$row['c_code']}?> <?php if($cate1 == $row['c_code']) echo " selected='selected' "; ?>><?=$row['c_name']?></option>
			<?php
			}
			?>
		</select>
	</span>
	<!--소분류-->
	<span style="float:left;width:150px;" id="cate_msg2"></span>
	<span style="float:left;width:300px;">
	<!--소분류-->
	카테고리로
	<button class="btn1" onclick="btn_check(this.form, 'move');">이동</button>
	<button class="btn1" onclick="btn_check(this.form, 'delete');">선택삭제</button>
	</span>
</div>
</form>

<script type="text/javascript">
<?php
if($cate_msg || $cate1)
{
?>
	get_category('<?=$cate1?>','<?=$cate_msg?>');
<?php
}
?>
</script>
<?
include_once ("./admin.tail.php");
?>